package com.blb.zycommunity.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zycommunity.entity.ResponseResult;
import com.blb.zycommunity.entity.SysOperLog;
import com.blb.zycommunity.entity.vo.SysOperLogExcelWrite;
import com.blb.zycommunity.entity.vo.SysOperLogVo;
import com.blb.zycommunity.mapper.SysOperLogMapper;
import com.blb.zycommunity.service.ISysOperLogService;
import com.blb.zycommunity.utils.ExcelDownloadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 操作日志记录 前端控制器
 * </p>
 *
 * @author chenheng
 * @since 2023-11-20
 */
@RestController
@CrossOrigin
public class SysOperLogController {

    @Resource
    private SysOperLogMapper sysOperLogMapper;

    @Autowired
    private ISysOperLogService iSysOperLogService;

    //查询所有加分页
    @PostMapping("log-page")
    public ResponseResult findPageAll(@RequestBody SysOperLogVo sysOperLogVo){
        Page<SysOperLog> page = new Page<>(sysOperLogVo.getPageNum(),sysOperLogVo.getPageSize());

        Page<SysOperLog> sysOperLogPage = sysOperLogMapper.selectPageVo(page,
                sysOperLogVo.getTitle(), sysOperLogVo.getOperName(),
                sysOperLogVo.getBusinessType(), sysOperLogVo.getStatus(),
                sysOperLogVo.getStartTime(),sysOperLogVo.getFinishTime());
        return ResponseResult.ok(sysOperLogPage);
    }





    @DeleteMapping("/operLog/{ids}")
    public ResponseResult deleteById(@PathVariable Long[] ids){
        for (Long id : ids) {
            iSysOperLogService.removeById(id);
        }
        return ResponseResult.ok("删除成功");
    }


    /**
     * 根据id导出数据
     * @param response
     * @param ids
     * @param filename
     * @throws IOException
     */
    @GetMapping("downloads")
    public void exports(
            HttpServletResponse response,
            @RequestParam("ids") long[] ids,
            @RequestParam("filename") String filename) throws IOException {
        System.out.println(filename);
        //List<Long> list = ExcelDownloadUtil.toList(ids);
        List<SysOperLog> sysOperLogs = new ArrayList<>();
        for (Long id : ids) {
            SysOperLog byId = iSysOperLogService.getById(id);
            sysOperLogs.add(byId);
        }
        sysOperLogs.forEach(System.out::println);
        ExcelDownloadUtil.excelDownload(response, filename, SysOperLogExcelWrite.class, sysOperLogs);
    }


}
